<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Storage;

/**
 * Class Keshi
 * @package App
 */
class Keshi extends Model
{
    public $timestamps = false;
    protected $table = 'tbl_keshi';


    const HOT_ON = 1;
    const HOT_OFF = 0;

    static $HotDisplay = [
        self::HOT_ON => '推荐',
        self::HOT_OFF => '不推荐',
    ];


    /**
     * 科室列表
     * @return \Illuminate\Support\Collection
     */
    static function Keshis()
    {
        return  Keshi::select('id', 'title')->get();
    }


    /**
     * 修改科室
     * @param $request
     * @param $keshi
     * @return array
     */
    static function ModifyKeshi($request, $keshi)
    {
        $keshi->title = $request->title;
        $keshi->weight = $request->weight;
        //是否推荐
        if ($request->has('hot')) {
            $keshi->hot = self::HOT_ON;
        } else {
            $keshi->hot = self::HOT_OFF;
        }

        //图片
        if ($request->has('pic')) {
            $pic = Storage::url(Postings::OssUpdate($request->pic));
            $keshi->pic = $pic;
        }

        if (!$keshi->save()) {
            return ['status' => 2, 'message' => '失败'];
        }

        return ['status' => 1, 'message' => '成功'];

    }


    /**
     * 添加科室
     * @param $request
     * @return array
     */
    static function KeshiPublish($request)
    {
        $keshi = new Keshi();
        $keshi->title = $request->title;
        $keshi->weight = $request->weight;
        $keshi->ctime = date('Y-m-d H:i:s', time());
        //是否推荐
        if ($request->has('hot')) {
            $keshi->hot = self::HOT_ON;
        } else {
            $keshi->hot = self::HOT_OFF;
        }

        //图片
        if ($request->has('pic')) {
            $pic = Storage::url(Postings::OssUpdate($request->pic));
            $keshi->pic = $pic;
        } else {
            return ['status' => 2, 'message' => '图片呢？'];
        }

        if (!$keshi->save()) {
            return ['status' => 2, 'message' => '失败'];
        }

        return ['status' => 1, 'message' => '成功'];

    }
}
